package com.cn.wanix.service.impl;

import com.cn.wanix.dao.UserDao;
import com.cn.wanix.dao.impl.UserImpl;
import com.cn.wanix.model.ResultModel;
import com.cn.wanix.model.UserModel;
import com.cn.wanix.service.UserService;
import com.cn.wanix.util.ResultTool;

/**
 * 服务逻辑层
 * 调用dao,通过sql语句来查询或者操作数据
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    @Override
    public ResultModel login(UserModel userModel) {
//        判断 当前用户名和密码是否正确，如果不正确，正确
        UserModel model = userDao.login(userModel);
        if (model != null) {
            return new ResultModel(ResultTool.CODE_SUCCES, ResultTool.MSG_QUERY_SUCCES);
        }
        return new ResultModel(ResultTool.CODE_ERROR, ResultTool.MSG_QUERY_ERROR);


    }

    @Override
    public ResultModel updateEnable(UserModel userModel) {
        return new ResultModel(userDao.updateEnable(userModel) > 0 ? ResultTool.CODE_SUCCES : ResultTool.CODE_ERROR, "");
    }

    @Override
    public ResultModel add(UserModel model) {
        int result = userDao.add(model);
        return ResultModel.getAdd(result);
    }

    @Override
    public ResultModel update(UserModel model) {
        int result = userDao.update(model);
        return ResultModel.getUpdate(result);
    }

    @Override
    public ResultModel delete(UserModel model) {
        int result = userDao.delete(model);
        return ResultModel.getDelete(result);
    }

    @Override
    public ResultModel<UserModel> findAll(UserModel model) {
        return new ResultModel(ResultTool.CODE_SUCCES, ResultTool.MSG_QUERY_SUCCES, userDao.getCount(model), userDao.findAll(model));
    }

    @Override
    public ResultModel<UserModel> findByID(UserModel model) {
        return new ResultModel(userDao.findByID(model));
    }

    @Override
    public ResultModel getCount(UserModel model) {
        return new ResultModel(userDao.getCount(model));
    }
}
